package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrCalcFeeUtil;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ServiceTimeAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.goods.GoodsHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author lyx
 * @version V1.0
 * @Description:
 * @date 2021/3/31
 * @Package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler
 */
@Service
@AttrHandler(ServiceTimeAttrHandler.class)
public class ServiceTimeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;
    @Autowired
    IQuerySceneInstService querySceneInstService;
    @Autowired
    IOperateSceneInstService operateSceneInstService;

    private static final String SERVICE_TERM_ATTR = "200004106";
    @Override
    public void handle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {
        // 不是框架的主产品不处理
        KiteOfferInst mainOfferInst = querySceneInstService.getGiftOfferInst(attrData.getSceneInstId());
        // 框架主产品的订购数量
        KiteAttr orderQuantityAttr = kiteInst.getAttr(SERVICE_TERM_ATTR);
        if (Objects.nonNull(mainOfferInst)&&GoodsHelper.isGift(mainOfferInst.getOfferId())&&Objects.nonNull(orderQuantityAttr)) {
            // 获取礼包下的所有主套餐
            List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInstsFromGiftOfferInst(attrData.getCustId(), mainOfferInst.getOfferInstId());
            if (KiteListUtils.isNotEmpty(kiteOfferInstList)) {
                kiteOfferInstList.forEach(kiteOfferInst -> {
                    // 获取该主套餐下的所有接入产品
                    List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(attrData.getCustId(), kiteOfferInst.getOfferInstId());
                    if (KiteListUtils.isNotEmpty(prodInstList)) {
                        prodInstList.forEach(kiteProdInst -> {
                            KiteAttr subServiceTermAttr = kiteProdInst.getAttr(SERVICE_TERM_ATTR);
                            if (Objects.nonNull(subServiceTermAttr)) {
                                subServiceTermAttr.setIsEdit("F");
                                //如果是智能烟感1 和 智能烟感2 则改为“36个月”'30000021','30000003'
                                if(KiteStringUtils.isNotBlank(kiteInst.getSpecId()) && KiteStringUtils.equalsAny(kiteInst.getSpecId(),"30000021","30000003") && KiteStringUtils.equalsAny(kiteProdInst.getProdId(),"50000083","50000020")){
                                    subServiceTermAttr.setValue("100000163");
                                    subServiceTermAttr.setValueDesc("36个月");
                                }else {
                                    if (!KiteStringUtils.equals(kiteInst.getSpecId(),"30000006")) {
                                        subServiceTermAttr.setValue(orderQuantityAttr.getValue());
                                        subServiceTermAttr.setValueDesc(orderQuantityAttr.getValueDesc());
                                    }
                                }
                                operateSceneInstService.setAttr(attrData.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), subServiceTermAttr);
                            }
                        });
                    }
                });
            }
        }



        /**
         * 标准销售价 200004107：16元/月（12月）；10元/月（24月）；8元/月（36月）
         * 标准结算价 200004108：7.9元/月（12月）；4.2元/月（24月）；2.9元/月（36月）
         * 产品公允价 200004115：7.9元/月（12月）；4.2元/月（24月）；2.9元/月（36月）
         */
        String salePrice = null;
        String settlementPrice = null;
        if (KiteStringUtils.equals(kiteAttr.getValue(), "100000161")) {
            // 12月
            salePrice = "16";
            settlementPrice = "7.9";
        }
        else if (KiteStringUtils.equals(kiteAttr.getValue(), "100000162")) {
            // 24月
            salePrice = "10";
            settlementPrice = "4.2";
        }
        else if (KiteStringUtils.equals(kiteAttr.getValue(), "100000163")) {
            // 36月
            salePrice ="8";
            settlementPrice = "2.9";
        }
        // I型Cat1电力模块
        if (KiteStringUtils.equals(kiteInst.getSpecId(), ProdCateUtil.getSubMemberProdId(ProdCateUtil.GIFT_ZNDB_PROD))) {
            operateInstAttrService.setAttrValue(kiteInst, "200004115", settlementPrice, attrData.getSceneInstId(), null);
        }
        // 物联网I型Cat1电力模块（主产品）
        if (KiteStringUtils.equals(kiteInst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_ZNDB_PROD))) {
            operateInstAttrService.setAttrValue(kiteInst, "200004107", salePrice, attrData.getSceneInstId(), null);
            operateInstAttrService.setAttrValue(kiteInst, "200004108", settlementPrice, attrData.getSceneInstId(), null);
        }

        String serviceTime = kiteAttr.getValue();
        String model = kiteInst.getAttrValue("200002772");
        if (KiteStringUtils.isNotBlank(model) && KiteStringUtils.isNotBlank(serviceTime)) {
            if (KiteStringUtils.equals(kiteInst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_JRBW_PROD))) {
                LocalAttrCalcFeeUtil.calculatePrice(model, serviceTime, kiteInst, attrData.getSceneInstId(), true);
            }
            else if (KiteStringUtils.equals(kiteInst.getSpecId(), ProdCateUtil.getSubMemberProdId(ProdCateUtil.GIFT_JRBW_PROD))) {
                LocalAttrCalcFeeUtil.calculatePrice(model, serviceTime, kiteInst, attrData.getSceneInstId(), false);
            }
        }

        // 物联网行车记录服务框架产品
        if (KiteStringUtils.isEqual(kiteInst.getSpecId(), ProdCateUtil.getMainMemberProdId(ProdCateUtil.GIFT_XCJL_PROD))) {
            // 12个月
            if (KiteStringUtils.isEqual(kiteAttr.getValue(),"100000161")) {
                // 标准销售价
                operateInstAttrService.setAttrValue(kiteInst, "200004107", "900",
                    attrData.getSceneInstId(), null);
                // 标准结算价
                operateInstAttrService.setAttrValue(kiteInst, "200004108", "750",
                    attrData.getSceneInstId(), null);

            }
            // 36个月
            if (KiteStringUtils.isEqual(kiteAttr.getValue(),"100000163")) {
                // 标准销售价
                operateInstAttrService.setAttrValue(kiteInst, "200004107", "1800",
                    attrData.getSceneInstId(), null);
                // 标准结算价
                operateInstAttrService.setAttrValue(kiteInst, "200004108", "1420",
                    attrData.getSceneInstId(), null);

            }
        }
        // 行车记录服务应用管理平台
        if (KiteStringUtils.isEqual(kiteInst.getSpecId(), ProdCateUtil.getSubMemberProdId(ProdCateUtil.GIFT_XCJL_PROD))) {
            // 12个月
            if (KiteStringUtils.isEqual(kiteAttr.getValue(),"100000161")) {
                // 产品公允价
                operateInstAttrService.setAttrValue(kiteInst, "200004115", "750",
                    attrData.getSceneInstId(), null);
                // 物资编码
                operateInstAttrService.setAttrValue(kiteInst, "200004178", "无",
                    attrData.getSceneInstId(), null);

            }
            // 36个月
            if (KiteStringUtils.isEqual(kiteAttr.getValue(),"100000163")) {
                // 产品公允价
                operateInstAttrService.setAttrValue(kiteInst, "200004115", "1420",
                    attrData.getSceneInstId(), null);
                // 物资编码
                operateInstAttrService.setAttrValue(kiteInst, "200004178", "无",
                    attrData.getSceneInstId(), null);

            }
        }
        //给标准结算价赋默认值，方便规则中心判断是否需要上传oop
        kiteInst.listAttrs().stream().forEach(instattr -> {
            if (KiteStringUtils.isEqual(instattr.getFieldName(),"200004108") && KiteStringUtils.isNotEmpty(instattr.getValue())){
                instattr.setDefaultValue(instattr.getValue());
            }
        });
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }
}
